﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class _3896Login : LoginImplBase
    {
        public _3896Login() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = String.Format("http://www.3896.com/user/denglu.aspx?login_name={0}&forget_pass={1}&che=false&url=", _username, _password);
            HttpResult resp = TransferMgr.doGetPure(loginurl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("alert") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            // 3896的cookie不符合规范, 因此需要单独处理
            //Cookie user_cookie = new Cookie();
            //user_cookie.Domain = "3896.com";
            //user_cookie.Path = "/";
            //user_cookie.Name = "user";
            //string cook_str = resp.getExtraHeader("Set-Cookie");
            //if(cook_str == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }
            //string[] cook_sub_strs = cook_str.Split(';');
            //if (cook_sub_strs.Length == 0) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }
            //string user_cookie_name_value = cook_sub_strs[0];
            //string cookie_value = user_cookie_name_value.Substring(user_cookie_name_value.IndexOf('=') + 1);
            //user_cookie.Value = cookie_value;
            //cookies.Add(user_cookie);

            string gamelisturl = "http://www.3896.com/game/serverlist.aspx?yid=502";
            resp = TransferMgr.doGetPure(gamelisturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGetServerList; return result; }

            string content = resp.getContent();

            // 查找serverid字符串
            string district_name = String.Format("\\){0}区", _acc.ServerId);
            Regex re = new Regex(String.Format("<a href=\"+?([^\"']*?)\"+?[^>]*?>[^<]*?{0}[^<]*?</a>", district_name));
            Match m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }

            string redirecturl = m.Groups[1].Value;
            if (!redirecturl.StartsWith("http")) redirecturl = "http://www.3896.com/game/" + redirecturl;

            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }

            processRedirect(redirecturl, result, ref cookies);
            return result;
        }
    }
}
